package com.dubito.composenote.ui.composable

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.dubito.composenote.R
import com.dubito.composenote.model.Note

/**
 * author: LiLiang
 * createTime: 2022/10/6-12:26
 * remark:
 **/

@Composable
fun GenericAppBar(
    title: String,
    onIconClick: () -> Unit,
    icon: @Composable (() -> Unit),
    iconState: MutableState<Boolean>
) {
    TopAppBar(
        title = { Text(text = title) },
        backgroundColor = MaterialTheme.colors.primary,
        actions = {
            IconButton(
                onClick = { onIconClick.invoke() },
                content = {
                    if (iconState.value) {
                        icon.invoke()
                    }
                }
            )
        }
    )
}

@Composable
fun NotesFloatingActionButton(
    contentDescription: String,
    action: () -> Unit,
    icon: ImageVector
) {
    FloatingActionButton(
        onClick = { action.invoke() },
        backgroundColor = MaterialTheme.colors.primary
    ) {
        Icon(imageVector = icon, contentDescription = contentDescription, tint = Color.Black)
    }
}

@Composable
fun DeleteDialog(
    openDialog: MutableState<Boolean>,
    text: MutableState<String>,
    action: () -> Unit,
    noteToDelete: MutableState<List<Note>>
) {
    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = {
                openDialog.value = false
            },
            title = {
                Text(text = stringResource(R.string.delete))
            },
            text = {
                Column {
                    Text(text = text.value)
                }
            },
            buttons = {
                Row(modifier = Modifier.padding(8.dp), horizontalArrangement = Arrangement.Center) {
                    Column {
                        Button(
                            onClick = {
                                action.invoke()
                                openDialog.value = false
                                noteToDelete.value = mutableListOf()
                            },
                            modifier = Modifier.fillMaxWidth(),
                            colors = ButtonDefaults.buttonColors(
                                backgroundColor = Color.Black,
                                contentColor = Color.White
                            )
                        ) {
                            Text(text = stringResource(R.string.confirm))
                        }
                        Spacer(modifier = Modifier.padding(12.dp))
                        Button(
                            onClick = {
                                openDialog.value = false
                            },
                            modifier = Modifier.fillMaxWidth(),
                            colors = ButtonDefaults.buttonColors(
                                backgroundColor = Color.Black,
                                contentColor = Color.White
                            )
                        ) {
                            Text(text = "取消")
                        }
                    }
                }
            }
        )
    }
}